'use strict';

$(function () {
    let edit = $('[value=编辑]');
    let del = $('[value=删除]');

    edit.click(btnEdit);
    del.click(btnDel);
})


function btnFind() {
    let keyword = $('[name=keyword]').val();
    let tr = $('.id1');
    let tbData = $('#tbData');
    keyword = !keyword ? "" : keyword;
    let pageIndex = 1;
    let pageSize = 20;


    $.ajax({
        url: `/student/find?keyword=${keyword}&pageIndex=${pageIndex}&pageSize=${pageSize}`,
        type: 'get',
        success: (res) => {
            tr.remove();

            res.data.forEach(item => {
                console.log(item);
                let html = `
                <tr class="id1">
                    <td>${item.id}</td>
                    <td>${item.studentName}</td>
                    <td>${item.birth}</td>
                    <td>${item.gender}</td>
                    <td>
                        <input type="button" value="编辑">
                        <input type="button" value="删除">
                    </td>
                </tr>
                `;
                tbData.append(html);
            })

        },
        error: (err) => {
            console.log(err);
        }
    })
}
function btnAdd() {
    console.log('添加');
    location.href = '/student/edit';
}
function btnEdit() {
    console.log('编辑');
    location.href = '/student/edit';
}

function btnDel(e) {
    console.log('删除');
    let res = confirm('你确定要删除吗？');
    if (res) {
        let currentTarget = e.currentTarget;
        let tr = $(currentTarget).parent().parent();
        let id = tr.find('td').first().text();

        $.ajax({
            url: '/student/del/' + id,
            type: 'delete',
            success: (res) => {
                console.log(res);
                tr.remove();
            },
            error: (err) => {
                console.log(err);
            }
        })


    }


}
function btnAdd(){
    
}
